import { Component, OnInit, Input, ElementRef, AfterViewInit, ViewChild, Output,
  EventEmitter, OnChanges, SimpleChanges, NgZone } from '@angular/core';
import { FSUtils, Fs } from '../../utils/fsutils';
import { BaseComponent } from '../base.component';
import { ElectronService } from '../../providers/electron.service';
import * as hljs from 'highlightjs';

@Component({
  selector: 'app-textview',
  templateUrl: './textview.component.html',
  styleUrls: ['./textview.component.scss']
})
export class TextviewComponent extends BaseComponent implements OnInit, OnChanges, AfterViewInit {
  isLoading: boolean;
  error: any;
  text: string;
  @ViewChild('content') preElement: ElementRef;
  @Input() filePath: string;
  @Input() content: string;
  @Output() whenCloseViewer = new EventEmitter();
  private isInited = false;
  constructor(electronService: ElectronService, el: ElementRef, private zone: NgZone) {
    super(electronService, el);
  }

  ngOnInit() {
    this.dataLoad();
    this.isInited = true;
  }
  ngOnChanges(simplechange: SimpleChanges) {
    if (simplechange.content || simplechange.filePath) {
      if (this.isInited) {
        this.dataLoad();
      }
    }
  }
  ngAfterViewInit() {
  }
  onCloseViwer() {
    if (this.whenCloseViewer) {
      this.whenCloseViewer.emit(this);
    }
  }
  dataLoad() {
    this.isLoading = true;
    const callback = (error, text?: string) => {
      if (error) {
        this.error = error;
      } else {
        this.text = text;
        let extension = 'txt';
        if (!this.content || this.content === '') {
          if (this.filePath && this.filePath.lastIndexOf('.') > -1) {
            extension = this.filePath.substring(this.filePath.lastIndexOf('.') + 1 ).toLowerCase();
          }
        }
        const highlightText = this.getHighlightText(extension, this.text);
        this.preElement.nativeElement.innerHTML = highlightText;
        // hljs.highlightBlock(this.content.nativeElement);
      }
      this.zone.run(() => {
        this.isLoading = false;
      });
    };
    if (!this.content || this.content === '') {
      if (FSUtils.fileExists(this.filePath)) {
        FSUtils.readTextAsync(this.filePath, callback);
      } else {
        callback(`${this.filePath} 不存在或已删除`, null);
      }
    } else {
      callback(null, this.content);
    }
  }
  private getHighlightText(extension: string, orginText: string) {
    // console.log(hljs);
    // console.log(hljs.listLanguages());
    let highlightText = '';
    if (extension === 'cs') {
      highlightText = hljs.highlight(extension, orginText).value;
    } else if (extension === 'html' || extension === 'htm') {
      highlightText = hljs.highlight('html', orginText).value;
    } else if (extension === 'json') {
      highlightText = hljs.highlight(extension, orginText).value;
    } else if (extension === 'sql') {
      highlightText = hljs.highlight(extension, orginText).value;
    }  else if (extension === 'xml') {
      highlightText = hljs.highlight(extension, orginText).value;
    } else if (extension === 'txt') {
      highlightText = hljs.highlight('tex', orginText).value;
    } else {
      highlightText = hljs.highlightAuto(orginText).value;
    }
    return highlightText;
  }
}
